﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace vote
{
    public partial class main : System.Web.UI.Page
    {
        const int PAGE_SIZE = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["currentPage"] = "0";
                int items = AccessDBUtil.ExecuteScalar("select count(*) from [voteTitle]");
                int pageLength = items % PAGE_SIZE == 0 ? items / PAGE_SIZE : (items / PAGE_SIZE + 1);
                ViewState["PageLength"] = pageLength.ToString();
            }

            int currentPage = Convert.ToInt32(ViewState["currentPage"]);

            string sql = getSql(currentPage);
            bindRepeater(sql);
        }

        private void bindRepeater(string sql)
        {
            DataSet ds = AccessDBUtil.ExecuteQuery(sql);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        /// <summary>
        /// 获取sql语句，因为第一页和其余页的sql语句是不同的
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <returns></returns>
        private string getSql(int currentPage)
        {
            if (currentPage == 0)
            {
                return "select top " + PAGE_SIZE + " * from [voteTitle] order by id desc";
            }
            else
            {
                return "select top " + PAGE_SIZE + " * from [voteTitle] where [id] not in (select top " +
                    PAGE_SIZE * currentPage + " [id] from [voteTitle]) order by id desc";
            }
        }

        #region repeater的一些事件
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            PlaceHolder ph = e.Item.FindControl("phOptions") as PlaceHolder;
            if (ph == null)
            {
                return;
            }

            string parentId = ((Label)e.Item.FindControl("lbId")).Text;
            SqlDataReader reader = AccessDBUtil.ExecuteReader("select * from [voteOption] where [parentId] = " + parentId + " order by id");

            if (bool.Parse(((Label)e.Item.FindControl("selMode")).Text))
            {
                RadioButtonList rbl = new RadioButtonList();
                rbl.DataTextField = "option";
                rbl.DataValueField = "id";
                rbl.DataSource = reader;
                rbl.DataBind();
                ph.Controls.Add(rbl);
            }
            else
            {
                CheckBoxList cbl = new CheckBoxList();
                cbl.DataTextField = "option";
                cbl.DataValueField = "id";
                cbl.DataSource = reader;
                cbl.DataBind();
                ph.Controls.Add(cbl);
            }
            reader.Close();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "poll")
            {
                PlaceHolder ph = e.Item.FindControl("phOptions") as PlaceHolder;
                Control option = ph.Controls[0];
                if (option is CheckBoxList)
                {
                    CheckBoxList cbl = (CheckBoxList)option;
                    foreach (ListItem li in cbl.Items)
                    {
                        if (li.Selected)
                        {
                            AccessDBUtil.ExecuteNonQuery("update [voteOption] set [ballots] = [ballots] + 1 where [id] = " + li.Value);
                        }
                    }
                    
                }
                else if (option is RadioButtonList)
                {
                    RadioButtonList rbl = option as RadioButtonList;
                    AccessDBUtil.ExecuteNonQuery("update [voteOption] set [ballots] = [ballots] + 1 where [id] = " + rbl.SelectedValue);
                }
                Response.Redirect("show.aspx?id=" + e.CommandArgument.ToString());
            }

        }
        #endregion
    }
}
